<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 



/**
* This function sends an email to a person using a template.
* Returns true on success and false if it can't send the email.
* 
* @param from_email 
* @param from_name
* @param to_email
* @param subject
* @param template
* @param template_data
* @return boolean
* 
*/

if ( ! function_exists('send_email'))
{      

      function send_email($from_email, $from_name, $to_email, $subject, $template, $template_data)
      {
          
          
          $CI =& get_instance();
        $fd = fopen ($template, "r");
        $mailcontent = fread ($fd, filesize ($template));
        
        foreach ($template_data as $key=>$value)
        {
            $mailcontent = str_replace("%%".$key."%%", $value,$mailcontent );
        }
        
        $mailcontent = stripslashes($mailcontent);
        
        fclose ($fd);
        
        // email information
        
        $CI->load->library('email');

        $CI->email->set_newline("\r\n"); 
        
        $CI->email->from($from_email, $from_name);
        $CI->email->to($to_email);
        $CI->email->subject($subject);
        
        $CI->email->message($mailcontent);
        // sending the email.
       
        if(!$CI->email->send()){
            echo $CI->email->print_debugger();
            $CI->email->clear();
            return FALSE;
        }else{
            return TRUE;
        }
    }
}
?>
